package com.example.myapplication.ArrayAdapterInclude;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.myapplication.ContentTest.ContactInfo;
import com.example.myapplication.R;
import java.util.ArrayList;

public class ContactArrayListAdapter extends ArrayAdapter<ContactInfo> {
  private ArrayList<ContactInfo> listData;
  private int convertViewId = -1;
  public ContactArrayListAdapter(Context context,ArrayList<ContactInfo> data) {
    super(context, R.layout.single_contact_info_layout, data);
    convertViewId=R.layout.single_contact_info_layout;
    listData=data;
  }

  @NonNull @Override
  public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    if (convertView == null) {
      convertView = LayoutInflater.from(getContext()).inflate(convertViewId, parent, false);

    }
    String str = ConvertText(getItem(position));
    TextView textView = (TextView) convertView.findViewById(R.id.infoView);
    textView.setText(str);
    return convertView;
  }

  private String ConvertText(ContactInfo item) {
    String name = item.getName();
    String number = item.getPhone();
    String convertStr = getContext().getString(R.string.contact_info, name, number);

    return convertStr;
  }
}
